package org.cn.springcloudvv.cxf.filter;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

/*
 * 用户会话信息过滤器
 * tangfuzhong
 */
@WebFilter(filterName = "sessionFilter",urlPatterns = {"/*"})
public class SessionFilter implements Filter {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest)servletRequest;
		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()){
			String headerName = headerNames.nextElement();
			logger.info("preHandle header中信息,headerName:{},headerValue:{}",headerName ,request.getHeader(headerName));
		}
		logger.info("preHandle requestURI:{}" , request.getRequestURI());
		logger.info("preHandle getMethod:{}" , request.getMethod());
		logger.info("preHandle queryString:{}",request.getQueryString());
		Map<String, String[]> parameterMap = servletRequest.getParameterMap();
		if (parameterMap.size()>0)
		{
			logger.info("请求参数{}", new Gson().toJson(parameterMap));
		}
		else
		{
			logger.info("请求参数：空");
		}

		filterChain.doFilter(servletRequest, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
